/**
 * SelectorServlet.java
 *
 * Copyright (c) 2009 viorelt All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of viorelt .
 **/
package ro.prjmanager.core.list;

import org.springframework.beans.BeanWrapperImpl;
import ro.prjmanager.core.annotations.Column;
import ro.prjmanager.core.annotations.SelectOption;
import ro.prjmanager.core.context.PrjManagerWebContextBuilder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

public class SelectorServlet extends GenericServlet {

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        ServletOutputStream out = response.getOutputStream();
        out.println(loadData(request));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        ServletOutputStream out = response.getOutputStream();
        out.println(loadData(request));
    }

    private String loadData(HttpServletRequest request) {
         PrjManagerWebContextBuilder.getBuilder().set(request,null);
        StringBuilder sb=new StringBuilder();
        sb.append("<select>");
        sb.append("<option value=''>-</option>");
        RequestParamList rpl = new RequestParamList(request);
        BeanWrapperImpl bean=new BeanWrapperImpl();
        try {
            Class clazz = Class.forName(rpl.getClassModel());
            Field[] fields=clazz.getDeclaredFields();
            String serviceName=null;
            String idOption=null;
            String nameOption=null;
            for(Field field:fields){
                Column col=field.getAnnotation(Column.class);
                if(col!=null){
                    SelectOption selectOption=field.getAnnotation(SelectOption.class);
                    if(selectOption!=null&&rpl.getSelectorName()!=null&&selectOption.id()!=null
                            &&rpl.getSelectorName().equals(selectOption.id())){
                        idOption=selectOption.idOption();
                        serviceName=selectOption.serviceName();
                        nameOption=selectOption.nameOption();
                    }
                }
            }

           List allSel=getService(request, serviceName).list();
           if(allSel!=null&&!allSel.isEmpty()){
               for(Object obj:allSel){
                   bean.setWrappedInstance(obj);

                   String idVal=bean.getPropertyValue(idOption).toString();
                   String nameVal=bean.getPropertyValue(nameOption)!=null?bean.getPropertyValue(nameOption).toString():"LABEL NOT SET";
                   sb.append("<option value='").append(idVal).append("'>").append(nameVal).append("</option>");
               }
           }

        } catch (ClassNotFoundException e) {
            log.error(e);
        }
        sb.append("</select>");
        return sb.toString();
    }

}
